package com.ruoyi.common.tools.excel.listener.asset;

import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.asset.cleaning.constant.SweepCleaningDirtConstant;
import com.ruoyi.asset.cleaning.domain.vo.SweepCleaningVo;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.erecord.region.domain.vo.RegionVo;
import com.ruoyi.erecord.region.service.IRegionService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 12276
 */
public class CleaningExcelListener extends MyExcelListener<SweepCleaningVo> {

    private DictService dictService = SpringUtils.getBean(DictService.class);

    private IRegionService regionService = SpringUtils.getBean(IRegionService.class);

    private String batchNo;
    private final String cityName = "深圳市";

    public CleaningExcelListener(String batchNo) {
        this.batchNo = batchNo;
    }

    @Override
    protected void doValidate(SweepCleaningVo data, AnalysisContext context) {
        String regionId = null, streetId = null;

        List<String> validateList = new ArrayList<>();

        if (StringUtils.isBlank(data.getRoadName())) {
            validateList.add("道路名称不能为空");
        }

        if (data.getArea() == null) {
            validateList.add("道路面积不能为空");
        } else if (data.getArea() < 0) {
            validateList.add("道路面积不能为负数");
        }

        if (!StringUtils.isEmpty(data.getRoadType())) {
            String lampType = this.dictService.getDictValue(SweepCleaningDirtConstant.CLEANING_TYPE, data.getRoadType());

            if (StringUtils.isNotBlank(lampType)) {
                data.setRoadType(lampType);
            } else {
                validateList.add("道路类型不存在");
            }
        } else {
            validateList.add("道路类型不能为空");
        }

        if (StringUtils.isNoneBlank(data.getRegion())) {
            // 获取区id
            regionId = regionService.getRegionIdByName(data.getRegion());
            RegionVo regionVo = regionService.queryById(regionId);

            if (regionVo != null && cityName.equals(regionVo.getParentTitle())) {
                data.setRegion(regionId);
            } else {
                validateList.add("区信息编辑不正确");
            }
        } else {
            validateList.add("区不能为空");
        }

        if (StringUtils.isNotBlank(regionId) && StringUtils.isNotBlank(data.getStreet())) {
            // 获街道id
            streetId = regionService.getRegionIdByName(data.getStreet());
            RegionVo regionVo = regionService.queryById(streetId);

            if (regionVo != null && regionId.equals(regionVo.getParentId())) {
                data.setStreet(streetId);
            } else {
                validateList.add("街道信息编辑不正确");
            }
        } else if (StringUtils.isBlank(data.getStreet())) {
            validateList.add("街道不能为空");
        }

        if (StringUtils.isNotBlank(streetId) && StringUtils.isNotBlank(data.getCommunity())) {
            // 获社区id
            String communityId = regionService.getRegionIdByName(data.getCommunity());
            RegionVo regionVo = regionService.queryById(communityId);

            if (regionVo != null && streetId.equals(regionVo.getParentId())) {
                data.setCommunity(communityId);
            } else {
                validateList.add("社区信息编辑不正确");
            }
        } else if (StringUtils.isBlank(data.getCommunity())) {
            validateList.add("社区不能为空");
        }

        if (StringUtils.isBlank(data.getDetailAddress())) {
            validateList.add("地址不能为空");
        }

        if (data.getServiceBeginDate() == null || data.getServiceEndDate() == null) {
            validateList.add("服务开始和服务结束时间不能为空");
        }

        if (!validateList.isEmpty()) {
            String errMsg = String.join(" ，", validateList);
            excelResult.getValidateList().add(errMsg);

            data.getDataCheckResult().setSucess(false);
            data.getDataCheckResult().setMsg(errMsg);
        } else {
            data.getDataCheckResult().setSucess(true);
        }
    }

    @Override
    protected void doOnExceptionProcess(String errorMsg) {

    }
}
